package com.tbm.console.proj.json;

import com.alibaba.fastjson.JSONArray;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.proj.entity.*;
import com.tbm.console.proj.service.*;
import com.tbm.console.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class ReportJson extends BaseController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportConfigInfoService reportConfigInfoService;

    @Autowired
    private TemplatePageService templatePageService;

    @Autowired
    private SystemMenuService systemMenuService;

    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/report-list/{page}/{size}")
    public Map<String,Object> localExcelColumnList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pageData = this.getPageData();
        pageData.put("menuType", "report");
//        pageData.put("deleteFlag", "N");
        long count = systemMenuService.count(pageData);
        List<SystemMenu> localExcelColumns = systemMenuService.findAll(pageData,page-1,size);
        List<SystemMenu> menuList = new ArrayList<>();
        for(SystemMenu systemMenu :localExcelColumns){
            String url = systemMenu.getMenuURL();
            systemMenu.setMenuURL(url.substring(8));
            menuList.add(systemMenu);
        }
        pageData.put("total",count);
        pageData.put("rows", menuList);
        return pageData;
    }

    /**
     * 常量增删改
     * @return
     */
    @RequestMapping(value = "/report-ins-upd-del")
    public Object logCreateUpdate(){
        PageData pageData = this.getPageData();
        Integer id = -1;
        if(null != pageData.get("id")&&!"".equals(pageData.get("id").toString())){
            id = Integer.valueOf(pageData.get("id").toString());
        }
        SystemMenu systemMenu = systemMenuService.findById(id);
        //------------------------------------------------------- 新增修改操作 ----------------------------------------------------------------------//
        if(-1==id){
            systemMenu.setCreateTime(DateUtil.getTime());
        }else{
            systemMenu.setUpdateTime(DateUtil.getTime());
        }
        //字段信息维护
        if(null!=pageData.get("firstParam")&&!"".equals(pageData.get("firstParam").toString())){
            //目前只能手动添加，这里添加所需要增加和修改的字段
            systemMenu.setMenuType("report");
            systemMenu.setMenuURL("/report/" + pageData.getString("firstParam"));
        }

        if(null!=pageData.get("descriptionCN")&&!"".equals(pageData.get("descriptionCN").toString())){
            systemMenu.setMenuName(pageData.get("descriptionCN").toString());
        }
        if(null!=pageData.get("parentId")&&!"".equals(pageData.get("parentId").toString())){
            systemMenu.setParentId((pageData.get("parentId").toString()));
        }
        systemMenuService.save(systemMenu);
        return null;
    }

    @RequestMapping("/report-parent-list")
    public Object adminGetRoleList(){
        List<PageData> pageDataList = new ArrayList<PageData>();
        PageData pd = new PageData();
        pd.put("parentId",0);
        List<SystemMenu> systemMenus = systemMenuService.findAll(pd);
        for(SystemMenu menu : systemMenus){
            PageData pageData = new PageData();
            pageData.put("value",menu.getId());
            pageData.put("text",menu.getMenuName());
            pageDataList.add(pageData);
        }
        return JSONArray.toJSONString(pageDataList);
    }

    /**
     * 生成报表
     * @return
     */
    @RequestMapping(value = "/report-generate")
    public void reportGenerate(){
        PageData pd = this.getPageData();

    }
}
